<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\wh\model\Color as ColorModel;
use app\wh\model\Stock as StockModel;
use think\Db;

/**
 * 物料模型
 */
class Material extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'wh_material';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //采购状态
    public static $status = [
        -1 =>     '审核未通过',
        0 =>      '待审核',
        1 =>      '未入库',
        2 =>      '仓库收货',
        3 =>      '已入库',
    ];
    //采购状态
    public static $status_bar = [
        0 =>      '发起采购',
        1 =>      '审核通过',
        2 =>      '仓库收货',
        3 =>      '已入库',
    ];
    //采购需求状态
    public static $urgent_status = [
        0 =>      '一般需求',
        1 =>      '紧急需求',
        2 =>      '加急需求',
    ];
    //采购打印条码状态
    public static $code_print_status = [
        0 =>      '待打印',
        1 =>      '已打印',
    ];
    //油温
    public static $heat = [
        0 =>      '无油温',
        1 =>      '中温',
        2 =>      '高温',
    ];
    //油温编码
    public static $heat_code = [
        0 =>      '',
        1 =>      'Z',
        2 =>      'G',
    ];
    //自定义状态标签
    public static function urgent_status_color($status=0,$label=''){
        switch ($status) {
            case '0': $class = ' bg-white';break;
            case '1': $class = ' bg-info-light';break;
            case '2': $class = ' bg-danger-light';break;
            case '3': $class = ' bg-danger-light';break;
            case '-1': $class = 'primary';break;
            case '-2': $class = 'primary';break;
            default: $class  = 'default';
        }
        $label = '<a class="btn btn-radius btn-'.$class.'">'.$label.'</a>';

        return $label;
    }
    //状态进度条
    public static function status_bar_tag($status=0){

        //进度条
        $status_bar = self::$status_bar;
        //审核状态
        if($status < 2)$status_bar[1] = self::$status[$status];
        //默认进度条颜色
        $class_tag = 'text-muted';
        $class_bar = 'text-info';
        $class_val = '';
        $class = [
            0       =>  [
                'bar'   =>  'text-info',
                'val'   =>  '',
            ]
        ];
        //根据状态，设置进度条颜色
        switch ($status) {
            case '0':
                $class[1] = [
                    'bar'   =>  'text-info',
                    'val'   =>  '',
                ];
                break;
            case '1':
                $class[1] = [
                    'bar'   =>  'text-info',
                    'val'   =>  'text-info',
                ];
                break;
            case '2':
                $class[1] = [
                    'bar'   =>  'text-info',
                    'val'   =>  'text-info',
                ];
                $class[2] = [
                    'bar'   =>  'text-info',
                    'val'   =>  'text-info',
                ];
                break;
            case '3':
                $class[1] = [
                    'bar'   =>  'text-info',
                    'val'   =>  'text-info',
                ];
                $class[2] = [
                    'bar'   =>  'text-info',
                    'val'   =>  'text-info',
                ];
                $class[3] = [
                    'bar'   =>  'text-info',
                    'val'   =>  'text-info',
                ];
                break;
            case '-1':
                $class[1] = [
                    'bar'   =>  'text-danger',
                    'val'   =>  'text-danger',
                ];
                break;
            default: ;
        }

        $thead = '<tr style="text-align: right">';
        $tbody = '<tr style="text-align: center">';
        foreach ($status_bar as $k=>$v){

            $class_tag_v = isset($class[$k]['bar'])?$class[$k]['bar']:$class_tag;
            $class_val_v = isset($class[$k]['val'])?$class[$k]['val']:$class_val;
            $class_bar_v = $class_tag_v==$class_tag?$class_tag_v:$class_bar;

            //进度条
            if($k > 0){
                $thead .= '<td colspan="2" class="'.$class_bar_v.'"><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i><i class="fa fa-tag-left2 fa-caret-right"></i></td>';
            }
            //图标
            if($k == 0)$thead .= '<td colspan="1" style="width: 30px"></td>';
            $thead .= '<td colspan="1" class="'.$class_tag_v.'"><i class="fa fa-lg fa-tag-left fa-tag-right fa-dot-circle-o"></i></td>';
            if ($k == (count($status_bar)-1))$thead .= '<td colspan="1"  style="width: 30px"></td>';
            //状态
            $tbody .= '<td colspan="3" class="'.$class_val_v.'">'.$v.'</td>';
        }
        $thead .= '</tr>';
        $tbody .= '</tr>';

        $label = '<table><tbody>'.$thead.$tbody.'</tbody></table>';

        return $label;
    }
    //获取库存
    public static function getSumNumber($data = []){

        $input['stock_sn'] = empty($data['stock_sn'])?'':$data['stock_sn'];
        $input['supplier_id'] = empty($data['supplier_id'])?'':$data['supplier_id'];
        $input['type_pid'] = empty($data['type_pid'])?'':$data['type_pid'];
        $input['type_id'] = empty($data['type_id'])?'':$data['type_id'];
        $input['model'] = empty($data['stock_sn'])?'':$data['model'];

        $where = [];
        if(!empty($input)){
            foreach ($input as $k=>$v){
                $where[] = [$k,'=',$v];
            }
        }

        if(!empty($where))$num = Db::name('wh_stock')->where($where)->value('number');
        if(empty($num))$num = '暂无库存';

        return $num;
    }
    // 采购单号
    public static function getMaterialSn(){

        $number = Db::name('wh_material')->order('id desc')->value('id');
        $number = intval($number)+1;

        return "CQZHC".date('Ymd').$number;
    }
    // 采购单表单设计
    public static function getTabFrom($pid = 0,$data = [],$print = 0){

        $from = [];

        switch ($pid) {
            case in_array($pid,ColorModel::$type_pid_arr)://油漆库
                if(empty($data['is_order_color']))$from = self::from1NotOrderColor($pid,$data,$print);
                elseif(empty($data['id']) && empty($data['order_ids']))$from = self::fromSelect($pid);
                else $from = self::from1($pid,$data,$print);
                break;
            case 3://原材料
                $from = self::from3($pid,$data,$print);
                break;
            case 4://铝卷
                $from = self::from4($pid,$data,$print);
                break;
            case 5://铝板
                $from = self::from5($pid,$data,$print);
                //$from = self::from4($pid,$data,$print);
                break;
            case 6://化工
            case 7://其他
                $from = self::from6($pid,$data,$print);
                break;
        }

        return $from;

    }

    //选择工程
    //print 0:编辑模式 1:打印模式 2:导出
    public static function fromSelect($type_pid = '',$isajax = false,$where = []){

        //---------------------------------------------工程表单项 start------------------------------------------
        $feild = 'selectorder';
        $feild2 = 'selectorder2';
        $table_head = [[
            "[12:1]
            <div id='form-ajax-search' action='".url('wh/material/selectorder',['type_pid'=>$type_pid])."' data-select='{$feild}' data-select2='{$feild2}'>
            ".self::inputToText('search','',0,'display:inline;width: 20%;float:left;','input','asearch form-ajax-search').
                    "
                    <a style='float:left;' class='btn btn-info notPrint' data-form='form-ajax-search' onclick=\"ajax_search(this)\" >搜索</a>
            </div>",
            ]];
        /*$table_head = [[
            "[2:1]
            <div id='form-ajax-search' action='".url('wh/material/selectorder',['type_pid'=>$type_pid])."' data-select='{$feild}' data-select2='{$feild2}'>
            ".self::inputToText('search','',0,'display:inline;width: 100%;','input','asearch form-ajax-search').
                    "
                    <a class='btn btn-info notPrint' data-form='form-ajax-search' onclick=\"ajax_search(this)\" >搜索</a>
            </div>",
            "[10:1]",
            ]];*/
        $table_data = [];

        $map = [];
        if(is_numeric($type_pid))$map[] = ['c.type_pid','=',$type_pid];

        if(empty($where)){
            $map[] = ['c.status','=',1];
            $where = '1=1';
        }
        $list = Db::name('mill_order')
            ->alias('o')
            ->field('o.*,mc.sales,sum(c.row_area) as color_row_area')
            ->leftJoin('wh_order_color c','o.id=c.order_id')
            ->leftJoin('mill_client mc','mc.id=o.client_id')
            ->where($map)
            ->where($where)
            ->group('o.id')
            ->order('o.id desc')
            ->select();
        //echo Db::name('mill_order')->getLastSql();exit;

        foreach ($list as $v){

            $client = Db::name('mill_client')->where(['id'=>$v['client_id']])->find();
            $row_area = Db::name('mill_order_archives')->where(['order_id'=>$v['id']])->sum('row_area');

            //定色列表
            $color_list = Db::name('wh_order_color')->where(['order_id'=>$v['id'],'type_pid'=>$type_pid])->select();
            $i = 1;
            $count = count($color_list);
            $supplier_color_html =
            $color_html =
            $area_html =
            $sum_number_html =
            $number_html = '<ul class="dch-ul">';
            foreach ($color_list as $c){
                //获取库存
                $c['sum_number'] = StockModel::getSumNumber($c);

                $class = 'dch-bottom';
                if($i == $count)$class = '';
                $supplier_color_html .= "<li class='dch-li {$class}'>厂家色号：<span class='tab-text-info'>{$c['supplier_color']}</span></li>";
                $color_html .= "<li class='dch-li {$class}'>颜色：<span class='tab-text-info'>{$c['color']}</span></li>";
                $area_html .= "<li class='dch-li {$class}'>面积：<span class='tab-text-info'>{$c['row_area']}</span></li>";
                $sum_number_html .= "<li class='dch-li {$class}'>库存：<span class='tab-text-info'>{$c['sum_number']}</span></li>";
                $number_html .= "<li class='dch-li {$class}'>定量：<span class='tab-text-info'>{$c['number']}</span></li>";

                $i++;
            }
            $supplier_color_html .= '</ul>';
            $color_html .= '</ul>';
            $area_html .= '</ul>';
            $sum_number_html .= '</ul>';
            $number_html .= '</ul>';

            $table_data[] = [
                "<div class='td-width' data-width='8px'><label class='css-input css-checkbox css-checkbox-primary'><input class='select-order-ids' onclick='select_order_ids(this)' data-select='{$feild}' data-select2='{$feild2}' type='checkbox' name='order_ids[]' value='{$v['id']}'><span></span></label></div>",
                "<div class='td-width hidden-border-left-right' data-width='250px'>档案号：<span class='tab-text-info'>{$v['archives_sn']}（{$v['batch']}）</span></div>",
                "<div class='td-width hidden-border-left-right' data-width='250px'>工程名称：<span class='tab-text-info'>{$v['engineering']}</span></div>",
                "<div class='td-width hidden-border-left-right' data-width='190px'>总面积：<span class='tab-text-info'>{$row_area}</span></div>",
                "<div class='td-width hidden-border-left-right' data-width='150px'>内勤：<span class='tab-text-info'>{$v['back']}</span></div>",
                "<div class='td-width hidden-border-left-right' data-width='190px'>销售经理：<span class='tab-text-info'>{$client['sales']}</span></div>",
                "<div class='td-width' data-width='290px'>{$supplier_color_html}</div>",
                "<div class='td-width' data-width='290px'>{$color_html}</div>",
                "<div class='td-width' data-width='170px'>{$area_html}</div>",
                "<div class='td-width' data-width='150px'>{$sum_number_html}</div>",
                "<div class='td-width' data-width='140px'>{$number_html}</div>",
                "<div class='td-width' style='width: 35px'><a style='margin-left: -10px;' class='btn btn-danger' data-order-id='{$v['id']}' data-href='".url('wh/material/rationSubs')."' onclick='select_order_del(this)'>取消</a></div>",
            ];




        }

        //设置表单项
        $from[] = ['complexTable:12', $feild2, '1/2请选择要采购的工程的内容', [], []];
        $from[] = ['complexTable:12', $feild, '', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //ajax模式
        if(!empty($isajax))return ['head'=>$table_head,'data'=>$table_data,'list'=>$list];
        return $from;
    }
    //折弯表单，不按确认颜色的工程创建
    //print 0:编辑模式 1:打印模式 2:导出
    public static function from1NotOrderColor($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [["[12:1]采购单号：{$data['material_sn']}<div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        //$table_head = [];
        $table_data = [];

        //批量打印
        $btn_html = "";
        if(!empty($data['id'])){
            $is_add = 'add';
            if($print)$is_add = $print;

            $ids = Db::name('wh_material_goods')
                ->alias('g')
                ->leftJoin('wh_stock s','s.stock_sn=g.stock_sn')
                ->where(['g.material_id'=>$data['id']])
                ->column('s.id');
            $btn['href'] = url('wh/stock/codePrint',['ids'=>implode(',',$ids)]);
            $btn['title'] = '批量打印';
            $btn_html .= self::showCodeHtml('',$is_add,[$btn]);
        }
        $btn_html .= "<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>";

        //汇总
        $row_area = 0;//合计面积
        $number = 0;//合计采购量
        $table_data[] = [
            "<div style='text-align: center;'>厂商色号</div>",
            "<div style='text-align: center;'>颜色名称</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>类别</div>",
            "<div style='text-align: center;'>类别2</div>",
            "<div style='text-align: center;'>类别3</div>",
            "<div style='text-align: center;'>油温</div>",
            "<div style='text-align: center;'>油漆类型</div>",
            "<div style='text-align: center;'>自编号</div>",
            "<div style='text-align: center;'>现有库存</div>",
            "<div style='text-align: center;'>采购量</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'></div>".$btn_html."</div>",
        ];
        $table_data2 = [];

        //厂家色号
        foreach ($data['lists'] as $vk=>$vv){

            $is_add = $print;
            if(empty($vv['id']))$is_add = 'add';

            $table_data[] = [
                "[1]
                    <div class='td-80'>
                        <input class='form-control goods' type='hidden' name='goods[id][]' value='{$vv['id']}'>
                        <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$vv['material_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[order_id][]' value='{$vv['order_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[supplier_id][]' value='{$vv['supplier_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$vv['type_pid']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_id][]' value='{$vv['type_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_id2][]' value='{$vv['type_id2']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_id3][]' value='{$vv['type_id3']}'>
                        <input class='form-control goods' type='hidden' name='goods[row_area][]' value='{$vv['row_area']}'>
                        <input class='form-control goods' type='hidden' name='goods[color_type_id][]' value='{$vv['color_type_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[color_sn][]' value='{$vv['color_sn']}'>
                        <input class='form-control goods' type='hidden' name='goods[stock_sn][]' value='{$vv['stock_sn']}'>
                        <input class='form-control goods' type='hidden' name='goods[order_color_id][]' value='{$vv['order_color_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[material_name][]' value='{$vv['material_name']}'>
                        <input class='form-control goods' type='hidden' name='goods[model][]' value='{$vv['model']}'>
                        <input class='form-control goods' type='hidden' name='goods[heat][]' value='{$vv['heat']}'>
                    
                    ".self::inputToText('goods[supplier_color][]',$vv['supplier_color'],$print,'display:inline;width: 70%;','input','color').
                "<a class='btn btn-success supplier-color color disable-dch pop' data-href='".url('supplierColorList',['_pop'=>1,'_parent_i'=>'__parent_i__'])."' data-layer=\"{&quot;area&quot;:[&quot;90%&quot;,&quot;95%&quot;],&quot;title&quot;:[&quot;请选择厂家色号&quot;,&quot;font-size:24px;text-align: center;&quot;]}\" onclick=\"supplier_color_list(this)\">查询</a></div>",
                "<div class='td-80 material_name'>{$vv['material_name']}</div>",
                "<div class='td-80 supplier_title'>".(empty($vv['supplier_id'])?'':$data['supplier'][$vv['supplier_id']])."</div>",
                "<div class='td-80 type_title'>".(empty($vv['type_id'])?'':$data['stock_type'][$vv['type_id']])."</div>",
                "<div class='td-80 type_title2'>".(empty($vv['type_id2'])?'':$data['stock_type2'][$vv['type_id2']])."</div>",
                "<div class='td-80 type_title3'>".(empty($vv['type_id3'])?'':$data['stock_type3'][$vv['type_id3']])."</div>",
                "<div class='td-80 heat_title'>".(empty($vv['heat'])?'':self::$heat[$vv['heat']])."</div>",
                "<div class='td-80 color_type_title'>".(empty($vv['color_type_id'])?'':$data['color_type'][$vv['color_type_id']])."</div>",
                "<div class='td-80 color_sn'>{$vv['color_sn']}</div>",
                "<div class='td-80 sum_number'>{$vv['sum_number']}</div>",
                "[0]
                    ".self::inputToText('goods[number][]',$vv['number'],$print,'display:inline;width: 80%;','input','goods')."
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                "[0]
                    <div class='td-80''>".self::showCodeHtml($vv['stock_sn'],$is_add)."</div>",
            ];

            $number += floatval($vv['number']);
        }

        //设置表单项
        $from[] = ['complexTable:12', 'order', '添加采购单信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //---------------------------------------------汇总表单项 start------------------------------------------
        $table_head2 = [['[8:1]']];

        $title = "合计：&nbsp;&nbsp;&nbsp;&nbsp;采购量：{$number}";
        $table_data2[] = [
            "[13:1]<div class='alert-info' style='text-align: right;height: 30px;font-size: 20px;'>{$title}</div>"
        ];
        $table_data2[] = [
            "[13:1]<div style='text-align: left;'>备注</div>"
        ];
        $table_data2[] = [
            "[13:1]".self::inputToText("note",$data['note'],$print,'height: 120px;','textarea')
        ];
        $table_data2[] = [
            "[1:1]<div style='text-align: right;'>审核人</div>",
            "[4:1]",
            "[1:1]<div style='text-align: right;'>审核时间</div>",
            "[4:1]",
        ];

        //设置表单项
        $from[] = ['complexTable:12', 'sum', '汇总', $table_data2, $table_head2];
        //---------------------------------------------汇总表单项 end--------------------------------------------

        //打印模式
        $table_data[] = ["[13:1]<div style='text-align: left;font-weight: 600;'><br>汇总</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data,$table_head2,$table_data2)];
        return $from;
    }
    //折弯表单，按确认颜色的工程创建
    //print 0:编辑模式 1:打印模式 2:导出
    public static function from1($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [["[13:1]采购单号：{$data['material_sn']}<div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        //$table_head = [];
        $table_data = [];

        //批量打印
        $btn_html = "";
        if(!empty($data['id'])){
            $is_add = 'add';
            if($print)$is_add = $print;

            $ids = Db::name('wh_material_goods')
                ->alias('g')
                ->leftJoin('wh_stock s','s.stock_sn=g.stock_sn')
                ->where(['g.material_id'=>$data['id']])
                ->column('s.id');
            $btn['href'] = url('wh/stock/codePrint',['ids'=>implode(',',$ids)]);
            $btn['title'] = '批量打印';
            $btn['class'] = 'btn-primary';
            $btn['style'] = 'margin-left: 30px;';
            $btn_html .= self::showCodeHtml('',$is_add,[$btn]);
        }

        //汇总
        $row_area = 0;//合计面积
        $number = 0;//合计采购量
        $table_head2[] = [
            "<div class='td-80' style='font-weight: 600;'>供应商</div>",
            "<div class='td-80' style='font-weight: 600;'>颜色名称</div>",
            "<div class='td-80' style='font-weight: 600;'>厂家色号</div>",
            "<div class='td-80' style='font-weight: 600;'>自编码</div>",
            "<div class='td-80' style='font-weight: 600;'>油温</div>",
            "<div class='td-80' style='font-weight: 600;'>面积</div>",
            "<div class='td-80' style='font-weight: 600;'>油漆类型</div>",
            "<div class='td-80' style='font-weight: 600;'>现有库存</div>",
            "<div class='td-80' style='font-weight: 600;'>采购量</div>",
            "<div class='td-80 notPrint' style='font-weight: 600;'>操作{$btn_html}</div>",
        ];
        $table_data2 = [];
        $table_total_data2 = [];

        //工程
        $i = 1;
        foreach ($data['lists'] as $k=>$v){
            if($k>0)$table_data[] = [];
            //工程信息
            $title = "<span style='color: black'>{$i}.档案号：</span>{$v['archives_sn']}  <span style='color: black'>工程名称：</span>{$v['engineering']}  <span style='color: black'>总面积：</span>{$v['row_area']}  <span style='color: black'>内勤：</span>{$v['back']}  <span style='color: black'>供应商：</span>{$v['supplier']}";
            $table_data[] = [
                "[13:1]<div class='alert-info goods{$v['id']}' style='text-align: left;height: 30px;font-size: 20px;'>{$title}<a style='float: right' class='btn btn-danger goods goods_del add-btn' data-order-id='{$v['id']}' data-order-ids='{$data['order_ids']}' onclick=\"order_del(this)\"><i class='fa fa-times'></i></a></div>"
            ];
            $table_data[] = [
                "[2]<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>厂家色号</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>自编码</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>油温</div>",
                "[3]<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>颜色名称</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>折弯类型</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>油漆类型</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>面积</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>现有库存</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color' style='font-weight: 600;'>采购量</div>",
                "<div class='td-80 goods{$v['id']} td-bg-color notPrint' style='font-weight: 600;'></div>",
            ];
            //厂家色号
            foreach ($v['lists'] as $vk=>$vv){
                $table_data[] = [
                    "[2]
                    <div class='td-80 goods{$v['id']}'>
                        <input class='form-control goods' type='hidden' name='goods[id][]' value='{$vv['id']}'>
                        <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$vv['material_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[order_id][]' value='{$vv['order_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[supplier_id][]' value='{$vv['supplier_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$vv['type_pid']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_id][]' value='{$vv['type_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_id2][]' value='{$vv['type_id2']}'>
                        <input class='form-control goods' type='hidden' name='goods[type_id3][]' value='{$vv['type_id3']}'>
                        <input class='form-control goods' type='hidden' name='goods[row_area][]' value='{$vv['row_area']}'>
                        <input class='form-control goods' type='hidden' name='goods[number][]' value='{$vv['number']}'>
                        <input class='form-control goods' type='hidden' name='goods[supplier_color][]' value='{$vv['supplier_color']}'>
                        <input class='form-control goods' type='hidden' name='goods[color_type_id][]' value='{$vv['color_type_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[color_sn][]' value='{$vv['color_sn']}'>
                        <input class='form-control goods' type='hidden' name='goods[stock_sn][]' value='{$vv['stock_sn']}'>
                        <input class='form-control goods' type='hidden' name='goods[order_color_id][]' value='{$vv['order_color_id']}'>
                        <input class='form-control goods' type='hidden' name='goods[material_name][]' value='{$vv['material_name']}'>
                        <input class='form-control goods' type='hidden' name='goods[model][]' value='{$vv['model']}'>
                        <input class='form-control goods' type='hidden' name='goods[heat][]' value='{$vv['heat']}'>
                    
                    {$vv['supplier_color']}
                    </div>",
                    "<div class='td-80'>{$vv['color_sn']}</div>",
                    "<div class='td-80'>".self::$heat[$vv['heat']]."</div>",
                    "[3]
                    <div class='td-80'>{$vv['material_name']}</div>",
                    "<div class='td-80'>{$vv['type_name']}</div>",
                    "<div class='td-80'>{$vv['color_type_name']}</div>",
                    "<div class='td-80'>{$vv['row_area']}</div>",
                    "<div class='td-80'>{$vv['sum_number']}</div>",
                    "<div class='td-80'>{$vv['number']}</div>",
                    "<div class='td-80 notPrint'><a style='margin-left: -10px;' class='btn btn-danger add-btn' data-order-id='{$v['id']}' data-order-color-id='{$vv['order_color_id']}' onclick='order_color_del(this)'>取消</a></div>",
                ];
                //汇总
                if($vv['number'] > 0)$row_area += $vv['row_area'];//合计面积
                $number += $vv['number'];//合计采购量
                if(empty($table_total_data2[$vv['supplier_id']][$vv['color_sn']]['row_area']))$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['row_area'] = 0;
                if(empty($table_total_data2[$vv['supplier_id']][$vv['color_sn']]['number']))$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['number'] = 0;
                $table_total_data2[$vv['supplier_id']][$vv['color_sn']]['row_area']  += $vv['row_area'];//合计面积
                $table_total_data2[$vv['supplier_id']][$vv['color_sn']]['number']  += $vv['number'];//合计采购量

                if(!isset($table_total_data2[$vv['supplier_id']][$vv['color_sn']]['count']))$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['count'] = 0;
                $table_total_data2[$vv['supplier_id']][$vv['color_sn']]['count']  ++;//计数

                $is_add = $print;
                if(empty($vv['id']))$is_add = 'add';

                //定义操作按钮
                $btn = [
                    [
                        'title'         =>  '修改',
                        'href'          =>  url('wh/material/editColor',['id'=>$vv['order_color_id'],'goods_id'=>$vv['id']]),
                    ]
                ];

                //无库存数据可编辑
                $color_add = $print;
                if(empty($vv['stock_sn']))$color_add = '';
                $table_total_data2[$vv['supplier_id']][$vv['color_sn']]['table'] = [
                    "[0]
                    <input class='form-control goods supplier_color' type='hidden' name='supplier_color[{$vv['color_sn']}][stock_sn]' value='{$vv['stock_sn']}'>
                    <input class='form-control goods supplier_color' type='hidden' name='supplier_color[{$vv['color_sn']}][color_sn]' value='{$vv['color_sn']}'>
                    <input class='form-control goods supplier_color' type='hidden' name='supplier_color[{$vv['color_sn']}][type_id]' value='{$vv['type_id']}'>
                    <input class='form-control goods supplier_color' type='hidden' name='supplier_color[{$vv['color_sn']}][supplier_color]' value='{$vv['supplier_color']}'>
                    <input class='form-control goods supplier_color' type='hidden' name='supplier_color[{$vv['color_sn']}][count]' value='{$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['count']}'>
                    
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][supplier_id]",$vv['supplier_id'],$color_add,'','select','goods supplier_color',$data['supplier'],'',['width'=>'170px']),
                    "[0]
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][color_title]",$vv['material_name'],1,'','input','goods supplier_color'),
                    "[0]
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][title]",$vv['supplier_color'],1,'','input','goods supplier_color'),
                    "[0]
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][color_sn]",$vv['color_sn'],1,'','input','goods supplier_color'),
                    "[0]
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][heat]",$vv['heat'],1,'','select','goods supplier_color',self::$heat),
                    "<div class='td-80'>{$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['row_area']}</div>",
                    "[0]
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][color_type_id]",$vv['color_type_id'],1,'','select','goods supplier_color',$data['color_type']),
                    "<div class='td-80'>{$vv['sum_number']}</div>",
                    "[0]
                    ".self::inputToText("supplier_color[{$vv['color_sn']}][number]",$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['number'],$color_add,'width: 110px;','input','goods supplier_color'),
                    "[0]
                    <div class='td-80'>".self::showCodeHtml($vv['stock_sn'],$is_add)."</div>",
                ];

                /*$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['table'] = [
                    "<div class='td-80''>{$vv['supplier']}</div>",
                    "<div class='td-80''>{$vv['material_name']}</div>",
                    "<div class='td-80''>{$vv['supplier_color']}</div>",
                    "<div class='td-80''>{$vv['color_sn']}</div>",
                    "<div class='td-80''>".self::$heat[$vv['heat']]."</div>",
                    "<div class='td-80''>{$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['row_area']}</div>",
                    "<div class='td-80''>{$vv['color_type_name']}</div>",
                    "<div class='td-80''>{$vv['sum_number']}</div>",
                    "<div class='td-80''>{$table_total_data2[$vv['supplier_id']][$vv['color_sn']]['number']}</div>",
                    "[0]
                    <div class='td-80''>".self::showCodeHtml($vv['stock_sn'],$is_add,$btn)."</div>",
                ];*/
            }

            $i++;
        }

        //遍历合计
        foreach ($table_total_data2 as $ttd){
            //汇总
            $row_area2 = 0;//合计面积
            $number2 = 0;//合计采购量
            foreach ($ttd as $ttdv){
                $table_data2[] = $ttdv['table'];
                //汇总
                $row_area2 += $ttdv['row_area'];//合计面积
                $number2 += $ttdv['number'];//合计采购量
            }
            $title = "合计：&nbsp;&nbsp;&nbsp;&nbsp;面积：{$row_area2}  采购量：{$number2}";
            $table_data2[] = [
                "[13:1]<div class='alert-info' style='text-align: right;height: 30px;font-size: 20px;'>{$title}</div>"
            ];
        }

        //设置表单项
        $from[] = ['complexTable:9', 'order', '2/2.查看选择的汇总信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //---------------------------------------------添加表单项 start------------------------------------------
        $table_head_add = [];
        $table_data_add = [];

        //可选择的工程
        $order_list = self::fromSelect($data['type_pid'],true)['list'];
        if(!empty($order_list)){
            foreach ($order_list as &$olv){
                $olv['title'] = "{$olv['archives_sn']}  {$olv['engineering']}";
                $olv['data_html'] = "data-engineering='{$olv['engineering']}' data-row-area='{$olv['color_row_area']}' data-back='{$olv['back']}' data-sales='{$olv['sales']}'";
            }
        }

        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>日期：</div>",
            "[0]
            ".self::inputToText('create_time',$data['create_time'],$print,'','date'),
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>到货时间：</div>",
            //"[0]".self::inputToText('address_time',$data['address_time'],$print,'','date'),
            "[0]
            ".self::inputToText('address_time',$data['address_time'],$print,'','date'),
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>状态：</div>",
            "[0]
            ".self::inputToText('urgent_status',$data['urgent_status'],$print,'','select','',self::$urgent_status),
        ];
        $table_data_add[] = [
            "[2:1]<div class='td-80' style='text-align: left;font-weight: 600'>添加采购条目</div>",
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>档案号：</div>",
            "[0]
            ".self::inputToText('','',$print,'','select','order_add',$order_list,'',['width'=>'250px']),
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>工程名称：</div>",
            "<div class='td-80 order_add_engineering'></div>",
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>总面积：</div>",
            "<div class='td-80 order_add_row_area'></div>",
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>销售经理：</div>",
            "<div class='td-80 order_add_sales'></div>",
        ];
        $table_data_add[] = [
            "<div class='td-80' style='text-align: right'>内勤：</div>",
            "<div class='td-80 order_add_back'></div>",
        ];
        $table_data_add[] = [
            "[2]
            <a style='width: 100%;' class='btn btn-primary order-add-a' onclick=\"order_add(this)\" data-type-pid='{$data['type_pid']}' data-id='{$data['id']}' data-order-ids='{$data['order_ids']}' data-order-id=''><i class=\"fa fa-plus-circle\"></i> 添加</a>",
        ];
        $from[] = ['complexTable:3', 'order_add', '', $table_data_add, $table_head_add];
        //---------------------------------------------添加表单项 end--------------------------------------------

        //---------------------------------------------汇总表单项 start------------------------------------------
        //$table_head2 = [['[8:1]汇总']];

        $title = "合计：&nbsp;&nbsp;&nbsp;&nbsp;面积：{$row_area}  采购量：{$number}";
        $table_data2[] = [
            "[13:1]<div class='alert-info' style='text-align: right;height: 30px;font-size: 20px;'>{$title}</div>"
        ];
        $table_data2[] = [
            "[13:1]<div style='text-align: left;'>备注</div>"
        ];
        $table_data2[] = [
            "[13:1]".self::inputToText("note",$data['note'],$print,'height: 120px;','textarea')
        ];
        $table_data2[] = [
            "[1:1]<div style='text-align: right;'>审核人</div>",
            "[4:1]",
            "[1:1]<div style='text-align: right;'>审核时间</div>",
            "[4:1]",
        ];

        //设置表单项
        $from[] = ['complexTable:9', 'sum', '汇总', $table_data2, $table_head2];
        //---------------------------------------------汇总表单项 end--------------------------------------------

        //打印模式
        $table_data[] = ["[13:1]<div style='text-align: left;font-weight: 600;'><br>汇总</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data,$table_head2,$table_data2)];
        return $from;
    }
    //原材料表单
    //print 0:编辑模式 1:打印模式
    public static function from3($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        if($print)$table_head = [["[7:1]采购单号：{$data['material_sn']}<div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        $table_data = [];

        //汇总
        $number = 0;//合计采购量
        $group = [];

        //批量打印
        $btn_html = "";
        if(!empty($data['id'])){
            $is_add = 'add';
            if($print)$is_add = $print;

            $ids = Db::name('wh_material_goods')
                ->alias('g')
                ->leftJoin('wh_stock s','s.stock_sn=g.stock_sn')
                ->where(['g.material_id'=>$data['id']])
                ->column('s.id');
            $btn['href'] = url('wh/stock/codePrint',['ids'=>implode(',',$ids)]);
            $btn['title'] = '批量打印';
            $btn_html .= self::showCodeHtml('',$is_add,[$btn]);
        }
        $btn_html .= "<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>";

        $table_head[] = [
            "<div style='text-align: center;'>物料类别</div>",
            "<div style='text-align: center;'>物料名称</div>",
            "<div style='text-align: center;'>规格型号</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>现有库存(kg)</div>",
            "<div style='text-align: center;'>需求量</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'></div>'.$btn_html.'</div>",
        ];

        foreach ($data['lists'] as $sak=>$sav){
            if(!empty($print) && 1==2){
                $table_data[] = [
                    "<div style='text-align: center;'></div>",
                ];
            }
            else{

                $is_add = $print;
                if(empty($vv['id']))$is_add = 1;

                $table_data[] = [
                    "[0]
                    <input class='form-control goods' type='hidden' name='goods[id][]' value='{$sav['id']}'>
                    <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$sav['material_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[order_id][]' value='{$sav['order_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$sav['type_pid']}'>
                    
                    ".self::inputToText('goods[type_id][]',$sav['type_id'],$print,'','select','goods',$data['stock_type']),
                    "[0]
                    ".self::inputToText('goods[material_name][]',$sav['material_name'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[model][]',$sav['model'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[supplier_id][]',$sav['supplier_id'],$print,'','select','goods',$data['supplier']),
                    "[0]
                    ".self::inputToText('goods[sum_number][]',$sav['sum_number'],$print,'display:inline;width: 80%;','input','goods').
                    "<a class='btn btn-success goods notPrint' onclick=\"sum_number_show(this)\" >查询</a>",
                    "[0]
                    ".self::inputToText('goods[number][]',$sav['number'],$print,'display:inline;width: 80%;','input','goods')."
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                    "[0]
                    <div class='td-80''>".self::showCodeHtml($sav['stock_sn'],$is_add)."</div>",
                ];
            }

            //汇总
            if(empty($group[$sav['model']]) && !empty($sav['model']))$group[$sav['model']] = 0;
            if (isset($group[$sav['model']]))$group[$sav['model']] += $sav['number'];

        }

        //设置表单项
        $from[] = ['complexTable:12', 'order', '添加采购单信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //---------------------------------------------汇总表单项 start------------------------------------------
        //$table_head2 = [['[8:1]汇总']];
        $table_head2[] = [
            "<div class='td-80'>规格型号</div>",
            "<div class='td-80'>需求总量</div>",
            "[5:1]<div class='td-80' style='width: 200px'></div>",
        ];

        if(!empty($group)){
            foreach ($group as $gk=>$gv){
                $table_data2[] = [
                    "<div class='td-80'>{$gk}</div>",
                    "<div class='td-80'>{$gv}</div>",
                    "[5:1]<div class='td-80' style='width: 200px'></div>",
                ];
            }
        }
        $table_data2[] = [
            "[7:1]<div style='text-align: left;font-weight: 600;'>备注</div>"
        ];
        $table_data2[] = [
            "[7:1]".self::inputToText("note",$data['note'],$print,'height: 120px;','textarea')
        ];
        $table_data2[] = [
            "[1:1]<div style='text-align: right;'>审核人</div>",
            "[2:1]",
            "[1:1]<div style='text-align: right;'>审核时间</div>",
            "[3:1]",
        ];

        //设置表单项
        $from[] = ['complexTable:12', 'sum', '汇总要采购的信息', $table_data2, $table_head2];
        //---------------------------------------------汇总表单项 end--------------------------------------------

        //打印模式
        $table_data[] = ["[7:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data,$table_head2,$table_data2)];
        return $from;
    }
    //铝卷表单
    //print 0:编辑模式 1:打印模式
    public static function from4($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [["[9:1]采购单号：{$data['material_sn']}<div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        $table_data = [];

        //汇总
        $num = 0;//合计数量
        $number = 0;//合计重量

        //批量打印
        $btn_html = "";
        if(!empty($data['id'])){
            $is_add = 'add';
            if($print)$is_add = $print;

            $ids = Db::name('wh_material_goods')
                ->alias('g')
                ->leftJoin('wh_stock s','s.stock_sn=g.stock_sn')
                ->where(['g.material_id'=>$data['id']])
                ->column('s.id');
            $btn['href'] = url('wh/stock/codePrint',['ids'=>implode(',',$ids)]);
            $btn['title'] = '批量打印';
            $btn_html .= self::showCodeHtml('',$is_add,[$btn]);
        }
        $btn_html .= "<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>";

        $table_data[] = [
            "<div style='text-align: center;'>名称</div>",
            "<div style='text-align: center;'>系列(类别)</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>规格型号</div>",
            "<div style='text-align: center;'>材质(合金状态)</div>",
            "<div style='text-align: center;'>批号</div>",
            "<div style='text-align: center;'>数量</div>",
            "<div style='text-align: center;'>米数(M)</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'></div>".$btn_html."</div>",
        ];

        foreach ($data['lists'] as $sak=>$sav){
            if(!empty($print) && 1==2){
                $table_data[] = [
                    "<div style='text-align: center;'></div>",
                ];
            }
            else{
                $is_add = $print;
                if(empty($sav['id']) || $sav['status'] == 2)$is_add = 1;
                $table_data[] = [
                    "[0]
                    <input class='form-control goods' type='hidden' name='goods[id][]' value='{$sav['id']}'>
                    <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$sav['material_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$sav['type_pid']}'>
                    <input class='form-control goods' type='hidden' name='goods[row_area][]' value=''>
                    <input class='form-control goods' type='hidden' name='goods[stock_sn][]' value=''>
                    
                    ".self::inputToText('goods[material_name][]',$sav['material_name'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[type_id][]',$sav['type_id'],$print,'','select','goods',$data['stock_type']),
                    "[0]
                    ".self::inputToText('goods[supplier_id][]',$sav['supplier_id'],$print,'','select','goods',$data['supplier']),
                    "[0]
                    ".self::inputToText('goods[model][]',$sav['model'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[quality][]',$sav['quality'],$print,'','select','goods',$data['quality']),
                    "[0]
                    ".self::inputToText('goods[lot_sn][]',$sav['lot_sn'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[num][]',$sav['num'],$print,'','input','goods'),
                    /*"[0]
                    ".self::inputToText('goods[sum_number][]',$sav['sum_number'],$print,'display:inline;width: 80%;','input','goods').
                    "<a class='btn btn-success goods notPrint' onclick=\"sum_number_show(this)\" >查询</a>
                    ",*/
                    "[0]
                    ".self::inputToText('goods[number][]',$sav['number'],$print,'display:inline;width: 80%;','input','goods')."
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                    "[0]
                    <div class='td-80''>".self::showCodeHtml($sav['stock_sn'],$is_add)."</div>",
                ];
            }

            $num += floatval($sav['num']);
            $number += floatval($sav['number']);

        }

        //设置表单项
        $from[] = ['complexTable:12', 'order', '添加采购单信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //---------------------------------------------汇总表单项 start------------------------------------------
        $table_head2 = [["[8:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"]];

        $title = "合计数量：{$num}  米数(M)：{$number}";
        $table_data2[] = [
            "[8:1]<div class='alert-info' style='text-align: right;height: 30px;font-size: 20px;font-weight: 600;'>{$title}</div>"
        ];
        $table_data2[] = [
            "[8:1]<div style='text-align: left;'>备注</div>"
        ];
        $table_data2[] = [
            "[8:1]".self::inputToText("note",$data['note'],$print,'height: 120px;','textarea')
        ];
        $table_data2[] = [
            "[1:1]<div style='text-align: right;'>审核人</div>",
            "[3:1]",
            "[1:1]<div style='text-align: right;'>审核时间</div>",
            "[3:1]",
        ];

        //设置表单项
        $from[] = ['complexTable:12', 'sum', '', $table_data2, $table_head2];
        //---------------------------------------------汇总表单项 end--------------------------------------------

        //打印模式
        $table_data[] = ["[6:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data,$table_head2,$table_data2)];
        return $from;
    }
    //铝板表单
    //print 0:编辑模式 1:打印模式
    public static function from5($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [["[9:1]采购单号：<span class='material_sn'>{$data['material_sn']} </span><input class='form-control' type='hidden' name='material_sn' value='{$data['material_sn']}'><div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        $table_data = [];

        //汇总
        $num = 0;//合计数量
        $number = 0;//合计重量
        $row_area = 0;//合计面积

        //批量打印
        $btn_html = "";
        if(!empty($data['id'])){
            $is_add = 'add';
            if($print)$is_add = $print;

            $ids = Db::name('wh_material_goods')
                ->alias('g')
                ->leftJoin('wh_stock s','s.stock_sn=g.stock_sn')
                ->where(['g.material_id'=>$data['id']])
                ->column('s.id');
            $btn['href'] = url('wh/stock/codePrint',['ids'=>implode(',',$ids)]);
            $btn['title'] = '批量打印';
            $btn_html .= self::showCodeHtml('',$is_add,[$btn]);
        }
        $btn_html .= "<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>";

        $table_data[] = [
            "<div style='text-align: center;'>名称</div>",
            "<div style='text-align: center;'>厚度</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>规格型号</div>",
            //"<div style='text-align: center;'>材质(合金状态)</div>",
            "<div style='text-align: center;'>数量</div>",
            "<div style='text-align: center;'>重量(KG)</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'></div>".$btn_html."</div>",
        ];

        foreach ($data['lists'] as $sak=>$sav){
            if(!empty($print) && 1==2){
                $table_data[] = [
                    "<div style='text-align: center;'></div>",
                ];
            }
            else{
                $is_add = $print;
                if(empty($sav['id']) || $sav['status'] == 2)$is_add = 1;
                $table_data[] = [
                    "[0]
                    <input class='form-control goods' type='hidden' name='goods[id][]' value='{$sav['id']}'>
                    <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$sav['material_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$sav['type_pid']}'>
                    <input class='form-control goods' type='hidden' name='goods[row_area][]' value=''>
                    <input class='form-control goods' type='hidden' name='goods[stock_sn][]' value=''>
                    <input class='form-control goods' type='hidden' name='goods[long][]' value='{$sav['long']}'>
                    <input class='form-control goods' type='hidden' name='goods[high][]' value='{$sav['high']}'>
                    
                    ".self::inputToText('goods[material_name][]',$sav['material_name'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[type_id][]',$sav['type_id'],$print,'','select','goods',$data['stock_type']),
                    "[0]
                    ".self::inputToText('goods[supplier_id][]',$sav['supplier_id'],$print,'','select','goods',$data['supplier']),
                    "[0]
                    ".self::inputToText('goods[model][]',$sav['model'],$print,'','input','goods'),
                    /*"[0]
                    ".self::inputToText('goods[quality][]',$sav['quality'],$print,'','select','goods',$data['quality']),
                    "[0]
                    ".self::inputToText('goods[lot_sn][]',$sav['lot_sn'],$print,'','input','goods'),*/
                    "[0]
                    ".self::inputToText('goods[num][]',$sav['num'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[number][]',$sav['number'],$print,'display:inline;width: 80%;','input','goods')."
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                    "[0]
                    <div class='td-80''>".self::showCodeHtml($sav['stock_sn'],$is_add)."</div>",
                ];
            }

            $num += floatval($sav['num']);
            $number += floatval($sav['number']);
            $row_area += floatval($sav['row_area']);

        }

        //设置表单项
        $from[] = ['file:12', 'material_file', '上传铝板计划表'];
        $from[] = ['select:6', 'supplier_list', '供应商','',$data['supplier']];
        $from[] = ['complexTable:12', 'order', '添加采购单信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //---------------------------------------------汇总表单项 start------------------------------------------
        $table_head2 = [["[8:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"]];

        $title = "合计数量：<span class='num_sum'>{$num} </span>  面积(M)：<span class='row_area_sum'>{$row_area}</span>  重量(KG)：<span class='number_sum'>{$number}</span>";
        $table_data2[] = [
            "[8:1]<div class='alert-info' style='text-align: right;height: 30px;font-size: 20px;font-weight: 600;'>{$title}</div>"
        ];
        $table_data2[] = [
            "[8:1]<div style='text-align: left;'>备注</div>"
        ];
        $table_data2[] = [
            "[8:1]".self::inputToText("note",$data['note'],$print,'height: 120px;','textarea')
        ];
        $table_data2[] = [
            "[1:1]<div style='text-align: right;'>审核人</div>",
            "[3:1]",
            "[1:1]<div style='text-align: right;'>审核时间</div>",
            "[3:1]",
        ];

        //设置表单项
        $from[] = ['complexTable:12', 'sum', '', $table_data2, $table_head2];
        //---------------------------------------------汇总表单项 end--------------------------------------------

        //打印模式
        $table_data[] = ["[6:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data,$table_head2,$table_data2)];
        return $from;
    }
    //化工表单
    //print 0:编辑模式 1:打印模式
    public static function from6($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [["[6:1]采购单号：{$data['material_sn']}<div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        $table_data = [];

        //汇总
        $number = 0;//合计采购量
        $group = [];

        //批量打印
        $btn_html = "";
        if(!empty($data['id'])){
            $is_add = 'add';
            if($print)$is_add = $print;

            $ids = Db::name('wh_material_goods')
                ->alias('g')
                ->leftJoin('wh_stock s','s.stock_sn=g.stock_sn')
                ->where(['g.material_id'=>$data['id']])
                ->column('s.id');
            $btn['href'] = url('wh/stock/codePrint',['ids'=>implode(',',$ids)]);
            $btn['title'] = '批量打印';
            $btn_html .= self::showCodeHtml('',$is_add,[$btn]);
        }
        $btn_html .= "<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>";

        $table_data[] = [
            "<div style='text-align: center;'>材质</div>",
            "<div style='text-align: center;'>规格型号</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>库存(kg)</div>",
            "<div style='text-align: center;'>需求量</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'></div>".$btn_html."</div>",
        ];

        foreach ($data['lists'] as $sak=>$sav){
            if(!empty($print) && 1==2){
                $table_data[] = [
                    "<div style='text-align: center;'></div>",
                ];
            }
            else{
                $is_add = $print;
                if(empty($sav['id']) || $sav['status'] == 2)$is_add = 1;
                $table_data[] = [
                    "[0]
                    <input class='form-control goods' type='hidden' name='goods[id][]' value='{$sav['id']}'>
                    <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$sav['material_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$sav['type_pid']}'>
                    <input class='form-control goods' type='hidden' name='goods[row_area][]' value=''>
                    
                    ".self::inputToText('goods[type_id][]',$sav['type_id'],$print,'','select','goods',$data['stock_type']),
                    "[0]
                    ".self::inputToText('goods[model][]',$sav['model'],$print,'','input','goods'),
                    "[0]
                    ".self::inputToText('goods[supplier_id][]',$sav['supplier_id'],$print,'','select','goods',$data['supplier']),
                    "[0]
                    ".self::inputToText('goods[sum_number][]',$sav['sum_number'],$print,'display:inline;width: 80%;','input','goods').
                    "<a class='btn btn-success goods notPrint' onclick=\"sum_number_show(this)\" >查询</a>
                    ",
                    "[0]
                    ".self::inputToText('goods[number][]',$sav['number'],$print,'display:inline;width: 80%;','input','goods')."
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                    "[0]
                    <div class='td-80''>".self::showCodeHtml($sav['stock_sn'],$is_add)."</div>",
                ];
            }

            //汇总
            if(empty($group[$sav['model']]) && !empty($sav['model']))$group[$sav['model']] = 0;
            if (isset($group[$sav['model']]))$group[$sav['model']] += $sav['number'];

        }

        //设置表单项
        $from[] = ['complexTable:12', 'order', '添加采购单信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //---------------------------------------------汇总表单项 start------------------------------------------
        //$table_head2 = [['[8:1]汇总']];
        $table_head2[] = [
            "<div class='td-80'>规格型号</div>",
            "<div class='td-80'>需求总量</div>",
            "[4:1]<div class='td-80' style='width: 200px'></div>",
        ];

        if(!empty($group)){
            foreach ($group as $gk=>$gv){
                $table_data2[] = [
                    "<div class='td-80'>{$gk}</div>",
                    "<div class='td-80'>{$gv}</div>",
                    "[4:1]<div class='td-80' style='width: 200px'></div>",
                ];
            }
        }
        $table_data2[] = [
            "[6:1]<div style='text-align: left;font-weight: 600;'>备注</div>"
        ];
        $table_data2[] = [
            "[6:1]".self::inputToText("note",$data['note'],$print,'height: 120px;','textarea')
        ];
        $table_data2[] = [
            "[1:1]<div style='text-align: right;'>审核人</div>",
            "[2:1]",
            "[1:1]<div style='text-align: right;'>审核时间</div>",
            "[2:1]",
        ];

        //设置表单项
        $from[] = ['complexTable:12', 'sum', '汇总要采购的信息', $table_data2, $table_head2];
        //---------------------------------------------汇总表单项 end--------------------------------------------

        //打印模式
        $table_data[] = ["[6:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data,$table_head2,$table_data2)];
        return $from;
    }

    // 查看条形码
    //print 0:编辑模式 1:打印模式
    public static function showCodeHtml($stock_sn = '',$print = 0,$btn = []){

        if($print && $print != 'add')return '';

        $html = '';
        if(!empty($btn)){
            foreach ($btn as $b){
                if(empty($b['class']))$b['class'] = ' bg-info-light ';
                if(empty($b['style']))$b['style'] = '';
                $layer = '{&quot;area&quot;:[&quot;80%&quot;,&quot;90%&quot;],&quot;title&quot;:[&quot;'.$b['title'].'&quot;,&quot;font-size:24px;text-align: center;&quot;]}';
                $html .= '<a class="btn disable-dch pop '.$b['class'].'" style="'.$b['style'].'" href="'.$b['href'].'?_pop=1" data-layer="'.$layer.'" _tag="archivesPrint" data-toggle="tooltip" data-original-title="'.$b['title'].'"><i class=""></i>'.$b['title'].'</a>';
            }
        }

        if(is_numeric($print)){
            $url_parm = ['stock_sn'=>$stock_sn];
            $url = url('wh/stock/codePrint',$url_parm)."?_pop=1";
            $layer = '{&quot;area&quot;:[&quot;780px&quot;,&quot;90%&quot;],&quot;title&quot;:[&quot;物料条形码&quot;,&quot;font-size:24px;text-align: center;&quot;]}';

            if(empty($extra['material_id']))$html .= '<a class="btn bg-info-light disable-dch pop" href="'.$url.'&print=0" data-layer="'.$layer.'" _tag="archivesPrint" data-toggle="tooltip" data-original-title="查看"><i class=""></i>查看</a>';
            $html .= '<a class="btn btn-primary disable-dch pop" href="'.$url.'" data-layer="'.$layer.'" _tag="archivesPrint" data-toggle="tooltip" data-original-title="打印"><i class=""></i>打印</a>';
        }

        return $html;
    }

    // 打印模板
    //print 1:打印模式 2:导出模式
    public static function toHtml($pid = 0,$data = [],$print = 1){

        $table_arr = self::getTabFrom($pid,$data,$print);

        //缩放比例
        $table_scaling = 1;

        //$html = '<html><style>th{font-size: 18px;font-weight: bold;}td{}</style><body style="margin: 4px;">';
        $html = '<html><style>th{font-size: 12px;font-weight: bold;}td{font-size: 12px;}</style><body style="margin: 4px;">';

        //头部
        /*$html .= '<table border="1" style="text-align: center;border-collapse: collapse;width: '.(21/$table_scaling).'cm;transform-origin: 0 0;"><thead>
                        <tr><th colspan="12" style="font-size: 25px;font-weight: bold;"><img style="width: 50px;margin-bottom: -20px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'</th></tr>
                    ';*/

        $colspan = 12;

        $html .= '<table border="1" style="text-align: center;border-collapse: collapse;transform-origin: 0 0;"><thead>
                        <tr><th colspan="'.$colspan.'" class="head" style="font-size: 25px;font-weight: bold;"><img style="width: 50px;margin-bottom: -20px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'</th></tr>
                    ';
        foreach ($table_arr as $tk=>$tv){
            //头
            if($tk == 'head'){
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tkkk=>$tvvv){

                        $v = self::tableParseCell($tvvv);

                        $html .= '
                            <th rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</th>
                            ';
                        if($tkkk == 0)$colspan = $v['colspan'];
                    }
                    $html .= '</tr>';
                }
                $html .= '</thead>';
            }
            //体
            elseif($tk == 'data'){
                $html .= '<tbody>';
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = self::tableParseCell($tvvv);

                        //$v['value'] = str_replace("\n","<br>",$v['value']);
                        $html .= '
                            <td rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</td>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</tbody>';
            }
        }
        $html .= '</table>';

        $js = 'window.print();';
        if($print == 2){
            $js = '$("table").table2excel({
                            // 不被导出的表格行的CSS class类
                            exclude: "",
                            // 导出的Excel文档的名称
                            name: "Excel Document Name",
                            // Excel文件的名称
                            filename: "采购单",
                            //文件后缀名
                            fileext: ".xls",
                            //是否排除导出图片
                            exclude_img: false,
                            //是否排除导出超链接
                            exclude_links: true,
                            //是否排除导出输入框中的内容
                            exclude_inputs: false
                        });';
        }
        //js
        $html .= '
                    <script src="'.config('template.tpl_replace_string')['__ADMIN_JS__'].'/core/jquery.min.js?v=20180327"></script>
                    <script src="'.config('template.tpl_replace_string')['__STATIC__'].'/common/js/jquery.table2excel.js?v='.date('Ymd').'"></script>
                    <script type="text/javascript">
                        //设置表头合并
                        $(".head").attr("colspan",'.$colspan.')
                        //删除不显示的元素
                        $(".notPrint").remove()
                        //去除边框
                        $(".td-none").parent().css("border-style","hidden")
                        //设置头部边框
                        $(".td-top").parent().css("border-top","black");
                        //设置底部边框
                        $(".td-bottom").parent().css("border-bottom","black");
                        //设置高度
                        $(".td-50").parent().css("height","50px");
                        //设置向下对齐
                        $(".text-bottom").parent().css("vertical-align","bottom");
                        //设置文字上下间距
                        $(".text-height").parent().css("line-height","28px");
                        //设置文字缩放比例
                        $("th,td").css("-webkit-transform","scale('.$table_scaling.')");
                        //设置文字缩放后左对齐
                        $(".scale-left").parent().css("transform-origin","0");
                        
                        '.$js.'
                    </script>';

        return $html;
    }

    // input框和纯文字互转
    public static function inputToText($field = '',$val = '',$print = 0,$style = '',$input = 'input',$class = '',$select = [],$tips = '',$extra = []){

        if($input == 'input')$input = 'text';
        $val_input = is_array($val)?'':$val;
        $re = "<input style='{$style}' class='form-control {$class}' type='{$input}' name='{$field}' value='".$val_input."'>";
        if($input == 'textarea')$re = "<textarea style='{$style}' class='form-control {$class}' name='{$field}'>{$val}</textarea>";
        elseif ($input == 'date')$re = "<input style='{$style}' class='js-datepicker form-control {$class}' type='text' id='{$field}' name='{$field}' value='".(!empty($val)?date('Y-m-d',$val):'')."' data-date-format='yyyy-mm-dd' placeholder='yyyy-mm-dd'>";
        elseif ($input == 'select'){
            $option = '<option></option>';
            foreach ($select as $k=>$v){
                $selected = '';
                $data_html = '';
                if(is_array($v)){
                    $k = $v['id'];
                    $data_html = $v['data_html'];
                    $v = $v['title'];
                }
                if($k == $val){
                    $selected = " selected ";
                    $val = $v;
                }
                $option .= '<option value="'.$k.'" '.$selected.$data_html.'>'.$v.'</option>';
            }
            $data_html = '';
            if(!empty($extra)){
                if(isset($extra['url'])){
                    $data_html .= " data-table='table' data-extra-param='' ";
                    //$data_html .= " data-url='{$extra['url']}' ";
                    $data_html .= " data-param='{$field}' ";
                    $data_html .= " data-next-items='{$extra['next_field']}' ";
                    $class .= ' select-linkage ';
                }
                foreach ($extra as $ek=>$ev){
                    $data_html .= " data-{$ek}='{$ev}' ";
                }
            }
            //$re = "<select class='js-select2 form-control {$class}' id='{$field}' name='{$field}' {$data_html} data-allow-clear='true' data-placeholder='请选择'>{$option}</select>";
            if(strpos($class, 'not-js-select') !== false)$class = "form-control {$class}";
            else $class = "js-select2 form-control {$class}";
            $re = "<select style='{$style}' class='{$class}' name='{$field}' {$data_html} data-allow-clear='true' data-placeholder='请选择'>{$option}</select>";
        }
        elseif ($input == 'checkbox'){
            $val_arr = $val;
            if(!is_array($val))$val_arr = explode(',',$val);
            $class = "css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded {$class}";
            $re = '';
            foreach ($select as $k=>$v){
                $checked = '';
                if(in_array($k,$val_arr))$checked = " checked ";
                $re .= "<label class='{$class}' style='{$style}'><input type='{$input}' name='{$field}[]' value='{$k}' {$checked}><span></span>{$v}</label>";
            }
        }
        if($tips)$re .= "<div class='help-block'>{$tips}</div>";

        if(!empty($print))return str_replace("\r","</br>",$val);

        return $re;
    }

    // 0：序列化 1：反序列化
    public static function serialize($lists = [],$type = 0,$validate = []){
        $data = [];

        //序列化
        if(empty($type)){
            foreach ($lists as $sk=>$sv){
                foreach ($sv as $skk=>$svv){
                    if(!empty($validate[$sk]) && empty($svv))return $validate[$sk];
                    $data[$skk][$sk] = $svv;
                }
            }
        }
        //反序列化
        else{
            foreach ($lists as $sk=>$sv){
                foreach ($sv as $skk=>$svv){
                    $data[$skk][$sk] = $svv;
                }
            }
        }

        return $data;
    }

    /**
     * 分析单元格合并
     * @param $v
     * @return array
     */
    public static function tableParseCell($v)
    {
        if (preg_match('/\[(.*)\]/', $v, $matches)) {
            $cell  = str_replace($matches[0], '', $v);
            $merge = explode(':', $matches[1]);
            $result = [
                'value'   => $cell,
                'colspan' => $merge[0],
                'rowspan' => isset($merge[1]) ? $merge[1] : '',
            ];
        } else {
            $result = [
                'value'   => $v,
                'rowspan' => '',
                'colspan' => '',
            ];
        }

        return $result;
    }
}
